package com.huawei.crm.web.domain;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 分页数据结果集
 *
 * @param <T>
 * @author luohua
 * @date 2021年12月30日17:19:41
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class PageResult<T> extends BaseResult {

    private static final long serialVersionUID = -8109739154376004134L;

    /**
     * 分页数据结果集
     */
    private PageInfo<T> data = null;

    public PageResult(int code, String message) {
        super(code, message);
    }

    public PageResult(int code, String message, PageInfo<T> data) {
        super(code, message);
        this.data = data;
    }

    public PageResult<T> code(Integer code) {
        this.setCode(code);
        return this;
    }

    public PageResult<T> message(String message) {
        this.setMessage(message);
        return this;
    }

    public PageResult<T> data(PageInfo<T> data) {
        this.setData(data);
        return this;
    }

    public static <T> PageResult<T> of(ResultCode status) {
        return new PageResult<>(status.code(), status.message());
    }

    public static <T> PageResult<T> success(PageInfo<T> data) {
        return new PageResult<>(ResultCode.SUCCESS.code(),
                ResultCode.SUCCESS.message(),
                data);
    }

    public static <T> PageResult<T> success(List<T> results) {
        return new PageResult<>(ResultCode.SUCCESS.code(),
                ResultCode.SUCCESS.message(),
                PageInfo.of(results));
    }

    public static <T> PageResult<T> success(List<T> results, Integer totalCount, Integer pageNum, Integer pageSize) {
        return new PageResult<>(ResultCode.SUCCESS.code(),
                ResultCode.SUCCESS.message(),
                PageInfo.of(results, totalCount, pageNum, pageSize));
    }

}
